package cn.jiahui;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;

import com.jfinal.kit.JsonKit;

public class LiftService extends Thread {
	private static boolean continueFlag = true;
	int port;
	int synInterval;
//	LiftRWThread liftRWThread;
	LiftReadThread liftReadThread;
	LiftWriteThread liftWriteThread;
	public final Object lock= new Object();
//	Socket socket = null;
//	String hostAddress = "";
	public LiftService(int port, int synInterval) {
		super();
		this.port = port;
		this.synInterval = synInterval;
	}

	
	public static void main(String[] args) {
		new LiftService(8088,1000).start();
	}

	@Override
	public void run() {
		ServerSocket ss = null;
		try {
			ss = new ServerSocket(port);
			while (continueFlag) {
				Socket s = ss.accept();
				System.out.println(s.getInetAddress().getHostAddress()+ " connected");
//				liftRWThread = new LiftRWThread(s);
//				new Thread(liftRWThread).start();
				liftReadThread = new LiftReadThread(s, this);
				liftWriteThread = new LiftWriteThread(s, this,synInterval);
				liftReadThread.start();
				liftWriteThread.start();
				
			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				ss.close();
			} catch (Exception e) {
				//
				e.printStackTrace();
			}
			ss = null;
		}
	}
	/**
	 * 设置按钮状态
	 * @param btnId 按钮id,上:1, 下2, 1楼:3 --- 8楼:10
	 * @param light 1为点亮,0为熄灭
	 */
	public boolean setBtnState(int btnId, int light){
		if(liftWriteThread == null){
			return false;
		}
		try {
			liftWriteThread.writeCommondForSetBtnState(btnId,light);
			return true;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}
	
	public LiftState getLiftState(){
		if(liftReadThread == null){
			return null;
		}
		byte[] listSateData = liftReadThread.listSateData;
		LiftState liftState = new LiftState();
		liftState.init(listSateData);
		
		
		return liftState;
	}
	
	public ArrayList<LiftBtnState> getBtnState() {
		if(liftReadThread == null){
			return null;
		}
		byte[] btnSateaData = liftReadThread.btnSateaData;
		ArrayList<LiftBtnState> btnStateArry = new ArrayList<LiftBtnState>();
		// -2 是因为两个字节来判断类型了.
		LiftBtnState btn = new LiftBtnState();
		btn.btnId = 10;
		btn.name = "cop8";
		btn.light = (btnSateaData[10 - 2] & (byte) 0X2) != 0 ? 1 : 0;
		btnStateArry.add(btn);

		btn = new LiftBtnState();
		btn.btnId = 9;
		btn.name = "cop7";
		btn.light = (btnSateaData[10 - 2] & (byte) 0X1) != 0 ? 1 : 0;
		btnStateArry.add(btn);

		btn = new LiftBtnState();
		btn.btnId = 8;
		btn.name = "cop6";
		btn.light = (btnSateaData[9 - 2] & (byte) 0X80) != 0 ? 1 : 0;
		btnStateArry.add(btn);

		btn = new LiftBtnState();
		btn.btnId = 7;
		btn.name = "cop5";
		btn.light = (btnSateaData[9 - 2] & (byte) 0X40) != 0 ? 1 : 0;
		btnStateArry.add(btn);

		btn = new LiftBtnState();
		btn.btnId = 6;
		btn.name = "cop4";
		btn.light = (btnSateaData[9 - 2] & (byte) 0X20) != 0 ? 1 : 0;
		btnStateArry.add(btn);

		btn = new LiftBtnState();
		btn.btnId = 5;
		btn.name = "cop3";
		btn.light = (btnSateaData[9 - 2] & (byte) 0X10) != 0 ? 1 : 0;
		btnStateArry.add(btn);

		btn = new LiftBtnState();
		btn.btnId = 4;
		btn.name = "cop2";
		btn.light = (btnSateaData[9 - 2] & (byte) 0X8) != 0 ? 1 : 0;
		btnStateArry.add(btn);

		btn = new LiftBtnState();
		btn.btnId = 3;
		btn.name = "cop1";
		btn.light = (btnSateaData[9 - 2] & (byte) 0X4) != 0 ? 1 : 0;
		btnStateArry.add(btn);

		btn = new LiftBtnState();
		btn.btnId = 2;
		btn.name = "hib_down";
		btn.light = (btnSateaData[9 - 2] & (byte) 0X2) != 0 ? 1 : 0;
		btnStateArry.add(btn);

		btn = new LiftBtnState();
		btn.btnId = 1;
		btn.name = "hib_up";
		btn.light = (btnSateaData[9 - 2] & (byte) 0X1) != 0 ? 1 : 0;
		btnStateArry.add(btn);
		 return btnStateArry;
	}
	
	
	

}
